package com.xiyi.shouxin.controller;

import java.util.List;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.xiyi.shouxin.controller.pojo.AnswerParams;
import com.xiyi.shouxin.controller.pojo.ListAnswerParams;
import com.xiyi.shouxin.controller.pojo.ListQuestionParams;
import com.xiyi.shouxin.entity.user.Question;
import com.xiyi.shouxin.entity.user.User;
import com.xiyi.shouxin.exception.ErrorCode;
import com.xiyi.shouxin.init.CacheUtil;
import com.xiyi.shouxin.service.QuestionService;
import com.xiyi.shouxin.utils.ExceptionUtil;
import com.xiyi.shouxin.view.CommonModel;
import com.xiyi.shouxin.view.pojo.ItemPage;
import com.xiyi.shouxin.view.pojo.QuestionView;
import com.xiyi.shouxin.view.pojo.UserAnswerView;

@CrossOrigin("*")
@RequestMapping("/question")
@RestController
public class QuestionController {
    private Gson gson = new Gson();
    @Autowired
    private Logger logger;
    @Autowired
    private CacheUtil cacheUtil;
    @Autowired
    private QuestionService questionService;

    @PostMapping("/list_question")
    public CommonModel listQuestion(@RequestBody ListQuestionParams params) {
        if (params == null || params.getPage() == null || params.getSize() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        try {
            ItemPage<QuestionView> info = questionService.findQuestionList(params.getStatus(),params.getPage(), params.getSize());
            return CommonModel.Success(info);
        } catch (Exception e) {
            logger.error("查询问题列表{}异常：", gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/create_question")
    public CommonModel createQuestion(@RequestBody Question params) {
        if (params == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        try {
            QuestionView info = questionService.createQuestion(params);
            return CommonModel.Success(info);
        } catch (Exception e) {
            logger.error("{}新增问题{}异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/update_question")
    public CommonModel updateQuestion(@RequestBody Question params) {
        if (params == null || params.getId() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        try {
            questionService.updateQuestion(params);
            return CommonModel.Success(null);
        } catch (Exception e) {
            logger.error("{}修改问题{}异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/del_question")
    public CommonModel delQuestion(@RequestBody List<Long> ids) {
        try {
            questionService.delQuestion(ids);
            return CommonModel.Success(null);
        } catch (Exception e) {
            logger.error("{}删除问题{}异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(ids), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/list_answer")
    public CommonModel listAnswer(@RequestBody ListAnswerParams params) {
        if (params == null || params.getPage() == null || params.getSize() == null) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        try {
            ItemPage<UserAnswerView> info = questionService.findAnswerList(params.getKeyword(), params.getPage(),
                    params.getSize());
            return CommonModel.Success(info);
        } catch (Exception e) {
            logger.error("{}查询用户回答列表{}异常：", gson.toJson(cacheUtil.getAdminOperator()), gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/answer")
    public CommonModel answer(@RequestBody AnswerParams params) {
        if (params == null || !StringUtils.hasText(params.getAnswer())) {
            return CommonModel.Fail(ErrorCode.INVALIDPARAMTER.getCode(), ErrorCode.INVALIDPARAMTER.getMsg(), null);
        }
        User user = cacheUtil.getUserOperator();
        if (user == null) {
            return CommonModel.Fail(ErrorCode.INVALIDTOKEN.getCode(), "请重新登录", null);
        }
        try {
            questionService.answerQuestion(user.getId(), params.getAnswer());
            return CommonModel.Success(null);
        } catch (Exception e) {
            logger.error("用户{}回答{}异常：", gson.toJson(user), gson.toJson(params), e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }
}
